/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-11-10
 */
@Controller
public class MenuInitController {
    // 超级管理员菜单
    private static final String[] ADMIN_ITEMS = { "userMgr", "roleMgr", "permissionMgr", "sysMenuMgr", "roleUserMgr",
            "rolePermitMgr", "permitMenuMgr" };
    private static final String[] ADMIN_ITEMS_NAME = { "用户管理", "角色管理", "权限管理", "系统菜单管理", "用户角色设置", "角色权限设置", "权限菜单设置" };
    private static final String[] ADMIN_ITEMS_URL = { "sysuser/userMgr", "sysrole/roleMgr",
            "syspermission/permissionMgr", "sysmenu/sysMenuMgr", "sysroleuser/roleUserMgr",
            "sysrolepermit/rolePermitMgr", "syspermitmenu/permitMenuMgr" };

    @RequestMapping("/tree/{id}")
    @ResponseBody
    public Object querySysMenu4Init(@PathVariable String id) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Object res = null;
        if ("root".endsWith(id)) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("$ref", "node1");
            map.put("name", "node1");
            map.put("children", true);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("$ref", "node2");
            map1.put("name", "node2");
            map1.put("url", "tree/root");
            list.add(map);
            list.add(map1);
            res = list;
        } else if ("node1".equals(id)) {
            Map<String, Object> map0 = new HashMap<String, Object>();
            map0.put("id", "node1");
            map0.put("name", "node1");
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("$ref", "node1.1");
            map.put("name", "node1.1");
            map.put("url", "tree/node1");
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("$ref", "node1.2");
            map1.put("name", "node1.2");
            map1.put("url", "tree/node1");
            list.add(map);
            list.add(map1);
            map0.put("children", list);
            res = map0;
        }
        return res;
    }

    @RequestMapping("/tree/admin/{id}")
    @ResponseBody
    public Object queryMgr4Admin(@PathVariable String id) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < ADMIN_ITEMS.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("$ref", ADMIN_ITEMS[i]);
            map.put("name", ADMIN_ITEMS_NAME[i]);
            map.put("url", ADMIN_ITEMS_URL[i]);
            list.add(map);
        }
        return list;
    }

    @RequestMapping("/zTree")
    public String zTree() {
        return "zTree";
    }

}
